<!--
	此示例下载自 https://echarts.apache.org/examples/zh/editor.html?c=line-smooth&code=PYBwLglsB2AEC8sDeAoWsAeBBDEDOAXMmurGAJ4gCmRA5AMYCGYVA5sAE7m0A0J6AE2aMiAbVoBZGL1i0AKgFcqM2gHUqAlXIAWClQDEOEFQGVmphdFoBdEgF8-6cjnxFUpMpRqyAbowA2SrT2jrB4VEZUhLCi_MQegsJiABwATAAMPLAAnADMqVnZ6QCMhbkALFnFqUVVubmZsMX56dahHhTUdP4Q0MrtpHgAtsDAYNpEYBxKcXYkbSRCYIwAWqNDBLEJ7gm7EAJ0S6vrABq8cbvond60eD0CEeeXCdi4eACS0A8YYq0DzwAzCD-FgcKQPOhAkGPC6kOboWx2ADcQA
	⚠ 请注意，该图表不是 Apache ECharts 官方示例，而是由用户代码生成的。请注意鉴别其内容。
-->
<!DOCTYPE html>
<html lang="zh-CN" style="height: 100%">
<head>
    <meta charset="utf-8">
    <title>曲线展示</title>
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="./Scripts/ECharts/echarts.min.js"></script>
<script type="text/javascript" src="./Scripts/ECharts/jquery.min.js"></script>
<!--    <script type="text/javascript" src="./data.js"></script>-->
<!--<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5.4.2/dist/echarts.min.js"></script>-->
<!-- Uncomment this line if you want to dataTool extension
<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5.4.2/dist/extension/dataTool.min.js"></script>
-->
<!-- Uncomment this line if you want to use gl extension
<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts-gl@2/dist/echarts-gl.min.js"></script>
-->
<!-- Uncomment this line if you want to echarts-stat extension
<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts-stat@latest/dist/ecStat.min.js"></script>
-->
<!-- Uncomment this line if you want to use map
<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@4.9.0/map/js/china.js"></script>
<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@4.9.0/map/js/world.js"></script>
-->
<!-- Uncomment these two lines if you want to use bmap extension
<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=YOUR_API_KEY"></script>
<script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5.4.2/dist/extension/bmap.min.js"></script>
-->

<script type="text/javascript">
    function getDataX() {
        return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    }
    function getDataY() {
        return [10, 25, 30, 25, 25, 50, 65, 68, 88, 95];
    }
    function onMouse(params) {
        // series name 为 'uuu' 的系列中的图形元素被 'mouseover' 时，此方法被回调。
        var dataPosition = {
            dataPosition: params.dataIndex,
            data: params.data
        };
        let url = "http://localhost:8008/dataPosition";
        $.ajax({
            url: url,
            method: "POST",
            data: JSON.stringify(dataPosition),
            contentType: "application/json",
            success: function (response) {
                console.log("位置上传成功");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log("上传失败: " + errorThrown);
            }
        });
    }
    const dom = document.getElementById('container');
    const myChart = echarts.init(dom, null, {
        renderer: 'canvas',
        useDirtyRect: false
    });
    const app = {};

    var option;

    option = {
        xAxis: {
            type: 'category',
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name:"line1",
                data: [],
                type: 'line',
                smooth: true
            }
        ],
        dataZoom: [
            {
                id: 'dataZoomX',
                type: 'slider',
                xAxisIndex: [0],
                filterMode: 'filter'
            }
        ]
    };

    if (option && typeof option === 'object') {
        myChart.setOption(option);
    }
    option = myChart.getOption();
    option.xAxis[0].data = getDataX();
    option.series[0].data = getDataY();
    myChart.setOption(option);

    myChart.on('mouseover', { seriesName: 'line1' }, function (params) {
        onMouse(params);
    });

    myChart.on('click', function (params) {
        if (params.componentType === 'markPoint') {
            // 点击到了 markPoint 上
            console.log('markPoint click');
            if (params.seriesIndex === 1) {
                // 点击到了 index 为 5 的 series 的 markPoint 上。
                console.log('markPoint line1 click');
            }
        } else if (params.componentType === 'series') {
            if (params.seriesType === 'line') {
                console.log('line1 click');
                onMouse(params);
            }
        }
    });

    setInterval(function () {
        let url = "http://localhost:8008/data.json";
        $.ajax({
            url: url,
            method: 'GET',
            success: function(response) {
                // 请求成功的处理逻辑
                //let jsonData = JSON.stringify(response);
                // 在这里可以使用JSON数据
                let sexOption = myChart.getOption();
                //var d=JSON.parse(jsonData);
                sexOption.series[0].data = response[0].Y;
                sexOption.xAxis[0].data = response[0].X;
                myChart.setOption(sexOption);

            },
            error: function(xhr, status, error) {
                // 请求失败的处理逻辑
                console.error('Error:', error);
            }
        });
    }, 2000);

    window.addEventListener('resize', myChart.resize);

</script>
</body>

</html>